﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlphaSolutionsGP
{
    public partial class frmQualificationView : Form
    {
        private Qualification aQualification;
        private string studentId;
        private bool toModify;

        public frmQualificationView(string studentID)
        {
            toModify = false;
            studentId = studentID;
            aQualification = new Qualification();
            InitializeComponent();
            dtAchieved.MaxDate = DateTime.Today;
        }

        //constructor with modifiable Event
        public frmQualificationView(Qualification q)
        {
            toModify = true;
            aQualification = q;
            InitializeComponent();
            printData();
            dtAchieved.MaxDate = DateTime.Today;
        }

        //method to print details of the qualification when in modify mode
        private void printData()
        {
            comboType.SelectedIndex = comboType.FindString(aQualification.Type);
            txtTitle.Text = aQualification.Title;
            comboSubject.SelectedIndex = comboSubject.FindString(aQualification.Subject);
            comboGrade.SelectedIndex = comboGrade.FindStringExact(aQualification.Grade);
            dtAchieved.Value = Convert.ToDateTime(aQualification.Date_Achieved);           
        }

        //display the message box for help
        private void bntHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You must fill in all the fields. \nIf you came to this window by modifying a qualification, remember to check all the details are correct before submitting. ", "WUC Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //qualification fields validation
        private bool qualificationFieldsValidation()
        {
            bool flag = true;
            string msg = "";
            if (comboType.Text == "")
            {
                msg += "\n- You must select a Type";
                flag = false;
            }
            if (comboSubject.Text == "")
            {
                msg += "\n- You must choose a Department";
                flag = false;
            }
            if (txtTitle.Text == "")
            {
                msg+="\n- The qualification Subject must not be blank";
                flag = false;
            }
            if (comboGrade.Text == "")
            {
                msg += "\n- You must choose a Grade";
                flag = false;
            }

            if (!flag)
                MessageBox.Show(msg, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
           
           return flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (qualificationFieldsValidation())
            {
                if (toModify)
                {
                    //update aQualification with new values
                    aQualification.Type = comboType.SelectedItem.ToString();                   
                    aQualification.Title = txtTitle.Text;
                    aQualification.Subject = comboSubject.SelectedItem.ToString();
                    aQualification.Grade = comboGrade.SelectedItem.ToString();
                    aQualification.Date_Achieved = dtAchieved.Text;
                    //this is where a qualification has been modified so needs to be put back into the arraylist.
                    aQualification.amendQualification();
                    this.Close();

                }
                //else new qualification and add
                else
                {
                    
                    Qualification theQualification = new Qualification(0, comboType.SelectedItem.ToString(), txtTitle.Text, comboSubject.SelectedItem.ToString(), comboGrade.SelectedItem.ToString(), dtAchieved.Text);
                    try
                    {
                        theQualification.createQualification(studentId);
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show("Failed to create the qualification.");
                    }

                }
            }
        }

        private void frmQualificationView_Load(object sender, EventArgs e)
        {

        }
    }
}
